<?php

use app\modules\Admin\models\storeapplyModel;
use Illuminate\Database\Capsule\Manager as DB;

#[门店申请管理]
class StoreapplyController extends AcommonController {

    public function init() {
        $this->model = (new storeapplyModel());
        $this->table = 'store_apply';
        $this->primaryKey = 'id';
        parent::init();
    }

    #[门店申请列表]
    public function indexAction($returnData = FALSE)
    {
        if($this->isPost()){
            $this->model = $this->model->with(['member']);
            $post_data = $this->postData;
            $search_data = [];
            if(isset($post_data['mode']) && !empty($post_data['mode']) && $post_data['mode'] != 'all'){
                switch ($post_data['mode']){
                    case 'waiting':
                        array_push($search_data, ['status', '=', 0]);
                        break;
                    case 'agreed':
                        array_push($search_data, ['status', '=', 1]);
                        break;
                    case 'refused':
                        array_push($search_data, ['status', '=', -1]);
                        break;
                }

            }
            if(isset($post_data['start_on']) && !empty($post_data['start_on'])){
                $this->model = $this->model->whereDate('created_at', '>=', $post_data['start_on']);
            }
            if(isset($post_data['end_on']) && !empty($post_data['end_on'])){
                $this->model = $this->model->whereDate('created_at', '<=', $post_data['end_on']);
            }
            $keywordsTypeArray = [
                'store_id' => 'store_id',
                'store_name' => 'store_name',
                'realname' => 'realname',
                'phone' => 'phone',
                'member_id' => 'member_id',
                'member_phone' => 'member_phone',
            ];
            if(isset($post_data['keywordsType']) && isset($post_data['keywords'])
                && in_array($post_data['keywordsType'], $keywordsTypeArray)
                && !empty($post_data['keywords'])){
                $keywords = $post_data['keywords'];
                if($post_data['keywordsType'] == $keywordsTypeArray['store_id'] && intval($keywords)){
                    array_push($search_data, ['id', '=', $keywords]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['store_name']){
                    array_push($search_data, ['name', 'like', "%{$keywords}%"]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['realname']){
                    array_push($search_data, ['realname', 'like', "%{$keywords}%"]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['phone']){
                    if(mb_strlen($keywords, 'utf-8') < 4){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        array_push($search_data, ['phone', 'like', "%{$keywords}%"]);
                    }
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['member_id'] && intval($keywords)){
                    array_push($search_data, ['member_id', '=', $keywords]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['member_phone']){
                    if(mb_strlen($keywords, 'utf-8') < 4){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        array_push($this->condition_in, ['member_id', memberModel::query()->where('phone', 'like', "%{$keywords}%")->pluck('id')->toArray()]);
                    }
                }
            }
            unset($keywordsTypeArray);
            $this->search = $search_data;
        }
        return parent::indexAction($returnData); // TODO: Change the autogenerated stub
    }

    #[查看申请]
    public function infoAction()
    {
        $id = $this->get('id', 0);
        if ($this->isPost()){
            if(!$id){
                ret(1, '参数出错！');
            }
            $infoModel = $this->model->with('member')->where($this->primaryKey, $id)->first()->append(['openweeks_text', 'openhours_text']);
            if(!$infoModel){
                ret(1, '要查看的门店申请不存在！');
            }
            ret(0, 'success', $infoModel);
        }else{
            $this->_view->assign('id', $id);
            $this->_view->display( strtolower($this->controller) . '/info.html');
        }
    }

    #[同意申请]
    public function applyconsentAction()
    {
        if(!$this->isPost()){
            ret(1, '请求方式出错');
        }
        if($this->isPost()){
            $id = $this->get('id', 0);
            if(empty($id)){
                ret(2, '请求参数出错');
            }
            if(!is_numeric($id)){
                ret(3, '参数类型出错');
            }
            $model = $this->model->where($this->primaryKey, $id)->first();
            if(!$model){
                ret(4, '申请记录不存在');
            }
            if($model->status != 0){
                ret(5, '申请记录已经被处理过');
            }
            try{
                $model->status = 1;
                $model->updated_at = timestamp();
                DB::transaction(function () use ($model) {
                    $model->save();
                    $store_id = storeModel::query()->insertGetId([
                        'name' => $model->name,
                        'images'  => $model->images,
                        'realname' => $model->realname,
                        'phone' => $model->phone,
                        'province_name' => $model->province_name,
                        'city_name' => $model->city_name,
                        'area_name' => $model->area_name,
                        'province_id' => $model->province_id,
                        'city_id' => $model->city_id,
                        'area_id' => $model->area_id,
                        'address' => $model->address,
                        'latitude' => $model->latitude,
                        'longitude' => $model->longitude,
                        'openhours' => $model->openhours,
                        'openweeks' => $model->openweeks,
                        'status' => 0,
                        'created_at' => timestamp(),
                    ]);
                    \app\modules\Admin\models\storememberModel::query()->insert([
                        'store_id' => $store_id,
                        'member_id' => $model->member_id
                    ]);
                });
                ret(0, '恭喜，同意门店申请操作成功！');
            }catch (Exception $e){
                ret(1, '抱歉，同意门店申请操作失败');
            }
        }
    }

    #[拒绝申请]
    public function applyrefuseAction()
    {
        if(!$this->isPost()){
            ret(1, '请求方式出错');
        }
        if($this->isPost()){
            $id = $this->get('id', 0);
            $reason = $this->get('remark', '无');
            if(empty($id)){
                ret(2, '请求参数出错');
            }
            if(!is_numeric($id)){
                ret(3, '参数类型出错');
            }
            $model = $this->model->where($this->primaryKey, $id)->first();
            if(!$model){
                ret(4, '申请记录不存在');
            }
            if($model->status != 0){
                ret(5, '申请记录已经被处理过');
            }
            try{
                $model->status = -1;
                $model->updated_at = timestamp();
                $model->reason = $reason;
                $model->save();
                ret(0, '恭喜，拒绝门店申请操作成功！');
            }catch (Exception $e){
                ret(1, '抱歉，拒绝门店申请操作失败');
            }
        }
    }
}
